"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能测试
Case Name   : truncate表样后执行to before truncate 进行表样闪回(ustore表)
Create At   : 2022/10/27
@zou_jialiang0501120111
Description :
    1.修改回收站参数enable_recyclebin为on
    2.创建表样并在表样内插入数据
    3.truncate表样,执行to before truncate进行表样闪回
    4.查看表样内数据
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class SQL(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Constant = Constant()
        self.Common = Common()
        self.table_name = 't_timecapsule_table_0006'
        self.default_value = \
            self.Common.show_param('enable_recyclebin')

    def test_timecapsule_table(self):
        step1 = '--step1:修改enable_recyclebin为on;expect:成功--'
        self.log.info(step1)
        mod_msg = self.primary_sh.execute_gsguc('set',
                                                self.Constant.GSGUC_SUCCESS_MSG,
                                                'enable_recyclebin =on')
        self.log.info(mod_msg)
        self.assertTrue(mod_msg, "执行失败" + step1)
        restart_msg = self.primary_sh.restart_db_cluster()
        self.log.info(restart_msg)
        sql_cmd = f'''show enable_recyclebin;'''
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn('on', result, "执行失败" + step1)

        step2 = '--step2:创建表样并在表样内插入数据;expect:成功--'
        self.log.info(step2)
        sql_cmd = f'''drop table if exists {self.table_name};
                      create table {self.table_name}
                      (
                         r_reason_sk    integer,
                         r_reason_id    character(16),
                         r_reason_desc  character(100)
                      )with (storage_type=ustore);
                     insert into {self.table_name} values (1, 'aa', 'reason1'),
                     (2, 'ab', 'reason2'),(3, 'ac', 'reason3');'''
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, result,
                      "执行失败" + step2)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, result,
                      '执行失败' + step2)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, result,
                      '执行失败' + step2)

        step3 = '--step3:truncate表样并执行to before truncate表样闪回;expect:成功--'
        self.log.info(step3)
        sql_cmd = f'''truncate table {self.table_name};
                      select pg_sleep(3);
                      timecapsule table {self.table_name} to before truncate;'''
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.Constant.TRUNCATE_SUCCESS_MSG,
                      result, '执行失败' + step3)
        self.assertIn('TimeCapsule Table', result, '执行失败' + step3)

        step4 = '--step4:查看表样内数据;expect:成功--'
        self.log.info(step4)
        sql_cmd = f'''select * from {self.table_name}'''
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn('reason1', result, '执行失败' + step4)
        self.assertEqual(result.count('3 row'), 1, '执行失败' + step4)

    def tearDown(self):
        step5 = '--step5:清理环境;expect:成功--'
        self.log.info(step5)
        clean_environment = self.primary_sh.execut_db_sql(
                            f'''drop table if exists {self.table_name};
                                purge recyclebin;''')
        self.log.info(clean_environment)
        mod_msg = self.primary_sh.execute_gsguc('set',
                                                self.Constant.GSGUC_SUCCESS_MSG,
                                                f'enable_recyclebin ='
                                                f'{self.default_value}')
        self.log.info(mod_msg)
        restart_msg = self.primary_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.primary_sh.get_db_cluster_status()
        self.assertIn(self.Constant.DROP_TABLE_SUCCESS, clean_environment,
                      "执行失败" + step5)
        self.assertIn('PURGE RECYCLEBIN', clean_environment,
                      "执行失败" + step5)
        self.assertTrue(mod_msg, "执行失败" + step5)
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + step5)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

